using System;
using System.Web.UI;
using Sedna.Core.Security;

namespace Sedna.Core.Controller.Configurations
{
	public class Primitives2StringTranslator : IDataTranslator
	{
        private bool clearIfZero = false;
        private IPrincipalWrapper principalWrapper;

        # region Properties

        public bool ClearIfZero
	    {
	        set { clearIfZero = value; }
        }

        public IPrincipalWrapper PrincipalWrapper
        {
            set { principalWrapper = value; }
        }

        # endregion

        public object Translate(Control control, object value)
		{
            string result = (value == null) ? "0" : Convert.ToString(value, principalWrapper.Culture);

            return (clearIfZero && result.Equals("0")) ? string.Empty : result;
		}

		public object Translate(Control control, object value, Type targetType)
		{
			if(targetType == typeof(Int32))
			{
				return string.Empty.Equals(value)?0:Int32.Parse((string)value);
			}
            if (targetType == typeof(Int64))
            {
                return string.Empty.Equals(value) ? 0 : Int64.Parse((string)value);
            }
            else if (targetType == typeof(double))
		    {
                return string.Empty.Equals(value) ? 0 : double.Parse((string)value);
		    }
			return value!=null?value.ToString():string.Empty;
		}
	}
}
